package similarity;

//import sun.plugin2.os.windows.FLASHWINFO;

import java.awt.*;
import java.util.ArrayList;


public class SimilarityMain {

	float sim(ArrayList<Float> va, ArrayList<Float> vb) {
		// 如果向量维度不相等，则不能计算，函数退出
		if (va.size() != vb.size()) {
			return 0;
		}
		/*作业，第三题*/

        float simVal = 0;

		float num = matrixMulti(va, vb); // numerator分子
		float den = pointMulti(va, vb); // denominator分母

		simVal = num / den;
		return simVal;
	}

	static float matrixMulti(ArrayList<Float> va, ArrayList<Float> vb) {
            float result = 0;
            for(int i=0; i<va.size(); i++) {
                result += va.get(i) * vb.get(i);
            }
            return result;
    }

    static float pointMulti(ArrayList<Float> va, ArrayList<Float> vb) {
        float vaSum = 0, vbSum = 0;
        for(Float n: va) {
            vaSum += Math.pow(n, 2);
        }
        for (Float n: vb) {
            vbSum += Math.pow(n, 2);
        }
        return (float)(Math.sqrt(vaSum) * Math.sqrt(vbSum));
    }

	public static void main(String[] args) {
		String item[] = {"吃苹果", "逛商店", "看电视剧", "打羽毛球", "吃桔子"};
		float a[] = {(float) 3.5, 5, 5, 5,0};
		float b[] = {3, 5, 4, 5, 5};
		ArrayList vitem = new ArrayList();
		ArrayList va = new ArrayList();
		ArrayList vb = new ArrayList();
		for (int i = 0; i < a.length; i++)
		{
			vitem.add(item[i]);
			va.add(new Float(a[i]));
			vb.add(new Float(b[i]));
		}
		System.out.print("兴趣");
		System.out.println(vitem);
		System.out.print("小红");
		System.out.println(va);
		System.out.print("zhiwei");
		System.out.println(vb);
		
		SimilarityMain sim = new SimilarityMain();
	
		float simVal = sim.sim(va, vb);
		
		System.out.println("The sim value is:" + simVal);
	}

}
